package com.mall.web.controller.business;

import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mall.common.annotation.Log;
import com.mall.common.enums.BusinessType;
import com.mall.business.domain.entity.BusCart;
import com.mall.business.service.BusCartService;
import com.mall.common.core.controller.BaseController;
import com.mall.common.core.domain.AjaxResult;
import com.mall.common.utils.poi.ExcelUtil;
import com.mall.common.core.page.TableDataInfo;

/**
 * 购物车Controller
 * 
 * @author zhonglin
 * @date 2020-05-24
 */
@Controller
@RequestMapping("/business/cart")
public class BusCartController extends BaseController
{
    private String prefix = "business/cart";

    @Autowired
    private BusCartService busCartService;

    @RequiresPermissions("business:cart:view")
    @GetMapping()
    public String cart()
    {
        return prefix + "/cart";
    }

    /**
     * 查询购物车列表
     */
    @RequiresPermissions("business:cart:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(BusCart busCart)
    {
        startPage();
        List<BusCart> list = busCartService.selectBusCartList(busCart);
        return getDataTable(list);
    }

    /**
     * 导出购物车列表
     */
    @RequiresPermissions("business:cart:export")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(BusCart busCart)
    {
        List<BusCart> list = busCartService.selectBusCartList(busCart);
        ExcelUtil<BusCart> util = new ExcelUtil<BusCart>(BusCart.class);
        return util.exportExcel(list, "cart");
    }

    /**
     * 新增购物车
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存购物车
     */
    @RequiresPermissions("business:cart:add")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(BusCart busCart)
    {
        return toAjax(busCartService.insertBusCart(busCart));
    }

    /**
     * 修改购物车
     */
    @GetMapping("/edit/{cartId}")
    public String edit(@PathVariable("cartId") Long cartId, ModelMap mmap)
    {
        BusCart busCart = busCartService.selectBusCartById(cartId);
        mmap.put("busCart", busCart);
        return prefix + "/edit";
    }

    /**
     * 修改保存购物车
     */
    @RequiresPermissions("business:cart:edit")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(BusCart busCart)
    {
        return toAjax(busCartService.updateBusCart(busCart));
    }

    /**
     * 删除购物车
     */
    @RequiresPermissions("business:cart:remove")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(busCartService.deleteBusCartByIds(ids));
    }
}
